package com.sk.sourcecircle.adapter;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.EMVideoMessageBody;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.callback.OnItemMessageClick;
import com.sk.sourcecircle.entity.MyEMMessage;

import java.io.File;
import java.util.List;

/**
 * Created by sk on 2018/1/5.
 */

public class ChatDetailsAdapter extends BaseMultiItemQuickAdapter<MyEMMessage, BaseViewHolder> {

    private OnItemMessageClick onItemMessageClick;
    private OnItemMessageClickListener onItemLongClickListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ChatDetailsAdapter(List<MyEMMessage> data, OnItemMessageClick onItemMessageClick,OnItemMessageClickListener onItemLongClickListener) {
        super(data);
        addItemType(1, R.layout.item_other_txt_message);
        addItemType(2, R.layout.item_other_image_message);
        addItemType(3, R.layout.item_other_voice_message);
        addItemType(4, R.layout.item_my_txt_message);
        addItemType(5, R.layout.item_my_image_message);
        addItemType(6, R.layout.item_my_voice_message);
        addItemType(7, R.layout.other_share_message);
        addItemType(8, R.layout.my_share_message);
        addItemType(9, R.layout.other_message_video);
        addItemType(10, R.layout.my_message_video);
        this.onItemMessageClick = onItemMessageClick;
        this.onItemLongClickListener = onItemLongClickListener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MyEMMessage item) {
        Glide.with(mContext).load(item.getEmMessage().ext().get("head_url")).apply(RequestOptions.bitmapTransform(new CircleCrop())).into((ImageView) helper.getView(R.id.head_img));
        helper.addOnClickListener(R.id.head_img);
        helper.getView(R.id.content).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemLongClickListener.longClickItem(helper.getAdapterPosition());
                return false;
            }
        });
        switch (item.getItemType()) {
            case 1:
                EMTextMessageBody body1 = (EMTextMessageBody) item.getEmMessage().getBody();
                helper.setText(R.id.content, body1.getMessage());
                break;
            case 2:
                EMImageMessageBody body2 = (EMImageMessageBody) item.getEmMessage().getBody();
                Glide.with(mContext).load(body2.getThumbnailUrl()).into((ImageView) helper.getView(R.id.content));
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(0, 2, helper.getAdapterPosition(), v);
                    }
                });
                break;
            case 3:
                EMVoiceMessageBody body3 = (EMVoiceMessageBody) item.getEmMessage().getBody();
                Log.v("EMAVoiceMessageBody", body3.getRemoteUrl());
                helper.setText(R.id.content, body3.getLength() + "s");
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(0, 3, helper.getAdapterPosition(), v);
                    }
                });


                break;
            case 4:
                EMTextMessageBody body4 = (EMTextMessageBody) item.getEmMessage().getBody();
                helper.setText(R.id.content, body4.getMessage());

                break;
            case 5:
                EMImageMessageBody body5 = (EMImageMessageBody) item.getEmMessage().getBody();
                if (body5.getLocalUrl() != null && new File(body5.getLocalUrl()).exists()) {

                    Glide.with(mContext).load(new File(body5.getLocalUrl())).into((ImageView) helper.getView(R.id.content));

                } else {
                    Glide.with(mContext).load(body5.getThumbnailUrl()).into((ImageView) helper.getView(R.id.content));
                }
                Log.v("EMImageMessageBody", body5.getThumbnailUrl());
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(1, 2, helper.getAdapterPosition(), v);
                    }
                });
                break;
            case 6:
                EMVoiceMessageBody body6 = (EMVoiceMessageBody) item.getEmMessage().getBody();
                Log.v("EMAVoiceMessageBody", body6.getRemoteUrl());
                helper.setText(R.id.content, body6.getLength() + "s");
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(1, 3, helper.getAdapterPosition(), v);
                    }
                });
                break;
            case 7:
                EMTextMessageBody body7 = (EMTextMessageBody) item.getEmMessage().getBody();
                helper.setText(R.id.title, body7.getMessage())
                        .setText(R.id.tv_content, item.getEmMessage().ext().get("describe") + "");
                Glide.with(mContext).load(item.getEmMessage().ext().get("imageUrl")).into((ImageView) helper.getView(R.id.image));
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(0, 4, helper.getAdapterPosition(), v);
                    }
                });
                break;
            case 8:
                EMTextMessageBody body8 = (EMTextMessageBody) item.getEmMessage().getBody();
                helper.setText(R.id.title, body8.getMessage())
                        .setText(R.id.tv_content, item.getEmMessage().ext().get("describe") + "");
                Glide.with(mContext).load(item.getEmMessage().ext().get("imageUrl")).into((ImageView) helper.getView(R.id.image));
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(1, 4, helper.getAdapterPosition(), v);
                    }
                });
                break;
            case 9:
                EMVideoMessageBody body9 = (EMVideoMessageBody) item.getEmMessage().getBody();
                if (body9.getLocalThumb() != null && new File(body9.getLocalThumb()).exists()) {
                    Glide.with(mContext).load(new File(body9.getLocalThumb())).into((ImageView) helper.getView(R.id.image_small));

                } else {
                    Glide.with(mContext).load(body9.getLocalThumb()).into((ImageView) helper.getView(R.id.image_small));
                }
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(0, 5, helper.getAdapterPosition(), v);
                    }
                });
                break;
            case 10:
                EMVideoMessageBody body10 = (EMVideoMessageBody) item.getEmMessage().getBody();
                if (body10.getLocalThumb() != null && new File(body10.getLocalThumb()).exists()) {

                    Glide.with(mContext).load(new File(body10.getLocalThumb())).into((ImageView) helper.getView(R.id.image_small));

                } else {
                    Glide.with(mContext).load(body10.getLocalThumb()).into((ImageView) helper.getView(R.id.image_small));
                }
                helper.getView(R.id.content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMessageClick.click(1, 5, helper.getAdapterPosition(), v);
                    }
                });

                break;
        }

    }

}
